<?php
class DnPipe
{
	public static function readwritefile($filename,$postdata)
	{
		$fp=fopen($filename, "r+");
		fwrite($fp,$postdata);
		$ret=fread($fp,65536);
		fclose($fp);
		return $ret;
	}
	
	public static function DoPipe($cmd)
	{
		static $com_name;
		if(!$com_name)$com_name=$_ENV["COMPUTERNAME"];//php_uname('n');
		
		$pipename="dnpipe".$_SERVER["REAL_PORT"];
		$data=self::readwritefile("\\\\" . $com_name. "\\pipe\\$pipename",json_encode($cmd)."\0");
		return $data;
	}
	public static function DoInit()
	{
		$cmd=array('act'=>'init');
		self::DoPipe($cmd);
		return;
	}
	public static function GetDrivers()
	{
		$cmd=array('act'=>'getdrivers');
		$str=self::DoPipe($cmd);
		$ret=explode("\0",$str);
		array_pop($ret);
		array_pop($ret);
		
		return $ret;
	}
	public static function GetIcon($filename)
	{
		$cmd=array('act'=>'geticon','file'=>$filename);
		$str=self::DoPipe($cmd);
		return $str;
	}
	public static function ExecFile($filename)
	{
		$cmd=array('act'=>'openfile','file'=>$filename);
		$str=self::DoPipe($cmd);
		return $str;
	}
	public static function GetAllCache()
	{
		$cmd=array('act'=>'getcache');
		$str=self::DoPipe($cmd);
		return $str;
	}
	public static function SetAllCache($data)
	{
		$cmd=array('act'=>'setcache','data'=>$data);
		$str=self::DoPipe($cmd);
		return $str;
	}
	public static function GetCache($key)
	{
		$cmd=array('act'=>'getcache');
		$alldata=self::DoPipe($cmd);
		$alldata=substr($alldata,0,-1); // trim \0
		$data=json_decode($alldata);
		
		if(!$data){return null;}
		if(!isset($data->$key)){return null;}
		return $data->$key;
	}
	public static function SetCache($key,$value)
	{
		$cmd=array('act'=>'getcache');
		$alldata=self::DoPipe($cmd);
		$data=json_decode($alldata);
		if(!$data){$data=array();}
		
		$data[$key]=$value;
		$data=json_encode($data);
		
		$cmd=array('act'=>'setcache','data'=>$data);
		self::DoPipe($cmd);
	}
	public static function OpenUrl($url)
	{
		$cmd=array('act'=>'openurl','url'=>$url);
		$str=self::DoPipe($cmd);
		return $str;
	}
	public static function OpenBrowser($url,$left,$top,$width,$height)
	{
		//$.get('index.php?act=openbrowser&left=100&top=200&width=300&height=400&url='+str,function(data){})

		$cmd=array('act'=>'openbrowser','left'=>''.$left,'top'=>''.$top,'width'=>''.$width,'height'=>''.$height,'url'=>$url);
		self::DoPipe($cmd);
	}
	
/*
	$cmd=$_GET;
	switch($cmd['act']){
		case 'init':
		case 'newwin':
			break;
		case 'openfolder':
			header("Content-Type:text/plain;charset=utf-8");
			echo DoPipe($cmd);exit;
			break;
		case 'capturescreen':
			header("Content-Type:image/bmp;charset=utf-8");
			echo DoPipe($cmd);exit;
			break;
		case 'geticon':
			header("Content-Type:image/bmp;charset=utf-8");
			echo DoPipe($cmd);exit;
			break;
		case 'openfile':
			header("Content-Type:text/plain;charset=utf-8");
			echo DoPipe($cmd);exit;
			break;
		case 'openbrowser':
			header("Content-Type:text/plain;charset=utf-8");
			echo DoPipe($cmd);exit;
			break;
		case 'getdrivers':
			header("Content-Type:text/plain;charset=utf-8");
			echo DoPipe($cmd);exit;
			break;

		default:
	}
*/
}